import QtQuick
import QtQuick.Layouts
import QtMultimedia
import QtQuick.Controls

import SnapyPairs

Item {
    id: game
    anchors.fill: parent

    required property int difficulty

    property GameState game_state: GameState {difty: difficulty}

    Image {
        id: replay
        x: 0
        y: 0
        source: 'media/images/return.png'

        MouseArea {
            anchors.fill: parent
            onClicked: {
                console.log('replay...')
                app_window.create_menu()
                game.destroy()
            }
        }
    }

    GridLayout {
        id: board
        anchors.centerIn: parent
        columns: game_state.column
        columnSpacing: 1
        rowSpacing: 3
        uniformCellWidths: true
        uniformCellHeights: true

        property list<int> selects
        property int count: 0
        property bool in_delay: false

        Timer {
            id: timer_delay
            interval: 1000
            repeat: false
            running: false

            onTriggered: {
                cards.itemAt(board.selects[0]).backed()
                cards.itemAt(board.selects[1]).backed()
                sound_player.stop()
                sound_player.play()

                board.in_delay = false
            }
        }

        Timer {
            id: timer_mem
            interval: 3000
            repeat: false
            running: false

            onTriggered: {
                for (var i = 0; i < cards.count; ++i) {
                    cards.itemAt(i).backed()
                }
            }
        }

        function check() {
            for (var i = 0; i < cards.count; ++i) {
                if (!cards.itemAt(i).is_front()) {
                    return false
                }
            }
            return true
        }

        Repeater {
            id: cards
            model: game_state.cards

            onModelChanged: {
                console.log('on-model-changed')
                timer_mem.start()
            }

            Card {
                required property int index
                required property string modelData

                front: path+'card'+modelData+'.png'
                source: front

                MouseArea {
                    anchors.fill: parent
                    enabled: !board.in_delay && parent.source == parent.back
                    onClicked: {
                        parent.fronted()
                        sound_player.stop()
                        sound_player.play()

                        board.selects[board.count++] = index

                        if (board.count == 2) {
                            board.count = 0
                            if (cards.itemAt(board.selects[0]).source == cards.itemAt(board.selects[1]).source) {
                                console.log("matched")
                                if (board.check()) {
                                    // completed current level
                                    console.log('clear level...');
                                    game_state.newLevel()
                                }
                            }
                            else {
                                console.log("not matched")
                                board.in_delay = true
                                timer_delay.start()
                            }
                        }
                    }
                }
            }
        }
    }

    Image {
        id: sound
        x: 870
        y: 380
        source: source_on

        property bool state: true
        property string source_on: 'media/images/audioOn.png'
        property string source_off: 'media/images/audioOff.png'

        MouseArea {
            anchors.fill: parent

            onClicked: {
                parent.state = !parent.state
                if (parent.state) {
                    sound_player._muted = false
                    parent.source = parent.source_on
                }
                else {
                    sound_player._muted = true
                    parent.source = parent.source_off
                }
            }
        }
    }

    Image {
        id: music
        x: 870
        y: 430
        source: source_on

        property bool state: true
        property string source_on: 'media/images/musicOn.png'
        property string source_off: 'media/images/musicOff.png'

        MouseArea {
            anchors.fill: parent

            onClicked: {
                parent.state = !parent.state
                if (parent.state) {
                    parent.source = parent.source_on
                    music_player.play()
                }
                else {
                    parent.source = parent.source_off
                    music_player.pause()
                }
            }
        }
    }

    MediaPlayer {
        id:  music_player
        source: 'media/music/Puzzle-Game-2.ogg'
        autoPlay:true
        loops: MediaPlayer.Infinite
        audioOutput: AudioOutput {}
    }

    MediaPlayer {
        id: sound_player
        source: 'media/sounds/cardPlace1.ogg'
        autoPlay: false
        loops: 1

        property bool _muted: false
        audioOutput: AudioOutput {
            muted: sound_player._muted
        }
    }
}
